<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Title</title>
    <link rel="stylesheet" href="../static/plugins/layui/css/layui.css">

</head>
<style>
    .ok-body {
        padding: 10px
    }
</style>

<body>
<div class="ok-body">

    <blockquote class="layui-elem-quote layui-text">
        用户信息管理
    </blockquote>


    <form class="layui-form layui-form-pane" action="">

        <input type="hidden" name="id" id="deviceId"  th:value="${id}" >

        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" lay-verify="required" autocomplete="off"  class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">登录名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="loginName"  autocomplete="off" lay-verify="required" class="layui-input">
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
                </div>
            </div>

        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <input type="hidden" name="organizationId" lay-verify="required">
                <label class="layui-form-label">岗位</label>
                <div class="layui-input-inline">
                    <input type="text" id="organizationName" lay-verify="required" style="background-color: #eee;" readonly class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">到岗时间</label>
                <div class="layui-input-inline">
                    <input type="text" id="arrivalTime" name="arrivalTime" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>

        </div>


        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">维修职称</label>
                <div class="layui-input-inline">
                    <select name="titleId" lay-verify="required" >
                        <option value=""></option>
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label" style="font-size: smaller;">取得证书时间</label>
                <div class="layui-input-inline">
                    <input type="text" id="certificateTime" name="certificateTime" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>

        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-inline">
                <select name="roleId" lay-verify="required" >
                    <option value=""></option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux">必选</div>
        </div>


        <div class="layui-form-item" style="width: 300px;" pane>
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <input type="checkbox" checked="" name="status" lay-skin="switch" value="1" lay-filter="switchTest" lay-text="启用|禁用">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>

</div>

<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/js/jquery-3.2.1.min.js"></script>
<script src="../static/js/jquery.base64.js"></script>
<script src="../static/js/md5.js"></script>
<script src="../static/js/device.js"></script>

<script th:inline="none">
    layui.use(['form','laydate'], function(){
        var form = layui.form;
        var laydate = layui.laydate;
        var tableName = 'user';
        var id = $('#deviceId').val();

        //到岗时间
        laydate.render({
            elem: '#arrivalTime'
            , format: 'yyyy-MM-dd'
        });

        //取得证书时间
        laydate.render({
            elem: '#certificateTime'
            , format: 'yyyy-MM-dd'
        });


        if(id){
            //表示是修改页面
            var param =  'current=1&pageSize=2&searchCol=id&searchValue='+id;
            JJZ.named('username').attr('readonly');
            JJZ.requestTableData(tableName,param,function(data){
                var result = data.records[0];
                JJZ.named('username').val(result.username);
                JJZ.named('loginName').val(result.loginName);
                JJZ.named('password').val(JJZ.SHOW_PASSWORD);
                JJZ.named('phone').val(result.phone);
                // JJZ.named('jobNum').val(result.jobNum);
                JJZ.named('arrivalTime').val(JJZ.FormatDate(result.arrivalTime, 'yyyy-MM-dd'));
                JJZ.named('certificateTime').val(JJZ.FormatDate(result.certificateTime, 'yyyy-MM-dd'));

                JJZ.named("organizationId").val(result.organizationId);
                $('#organizationName').val(result.organizationName);

                //初始角色下拉框
                JJZ.initSelect(JJZ.named('roleId'), '/role/dataGrid', function (noError) {
                    if (noError) {
                        JJZ.named('roleId').find('option').each(function (index, elem) {
                            $(elem).val() == result.roleId ? $(elem).attr("selected", "selected") : '';
                        });
                        form.render(); //更新表单
                    }
                });

                JJZ.initSelect(JJZ.named('titleId'), '/title/dataGrid', function (noError) {
                    if (noError) {
                        JJZ.named('titleId').find('option').each(function (index, elem) {
                            $(elem).val() == result.titleId ? $(elem).attr("selected", "selected") : '';
                        });
                        form.render();
                    }
                });

                result.status == 1? '': JJZ.named('status').removeAttr("checked");
                form.render(); //更新表单
            });

        }else{

            JJZ.named("organizationId").val(0);
            //初始角色下拉框
            JJZ.initSelect(JJZ.named('roleId'), '/role/dataGrid', function (noError) {
                form.render(); //更新表单
            });
            JJZ.initSelect(JJZ.named('titleId'), '/title/dataGrid', function (noError) {
                form.render(); //更新表单
            });

        }

        form.on('submit(submit)', function(data){
            data.field.status = data.field.status==1?1:0;
            if(id){
                //修改
                JJZ.editData(tableName,data.field,function(data){
                    window.location.href =JJZ.baseUrl+ '/'+tableName+'/userDataGrid';
                });
            }else{
                //新增
                JJZ.addData(tableName,data.field,function(data){
                    window.location.href =JJZ.baseUrl+ '/'+tableName+'/userDataGrid';
                });
            }
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        /*部门管理-新增-选择父部门树*/
        $('#organizationName').click(function () {
            var parentId = JJZ.named('organizationId').val();

            parent.layer.open({
                type: 2,
                maxmin: true,
                shade: 0.3,
                fix: false,
                area: ['400px','700px'],
                content: ['organization/tree?id='+parentId,'no'],
                shadeClose: true,
                btn: ['<i class="fa fa-check"></i> 确认', '<i class="fa fa-close"></i> 关闭'],
                yes: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    JJZ.named('organizationId').val(body.find('#treeId').val());
                    $("#organizationName").val(body.find('#treeName').val());
                    layer.close(index);
                }, cancel: function () {
                    return true;
                }
            });
        }) ;


    });
</script>
</body>
</html>